# 读取文件全部读取
# with open('pi_digits.txt') as file_object:
#     contents = file_object.read()
#     print(contents)


# 逐行读取
# with open('pi_digits.txt') as file_object:
#     for line in file_object:
#         print(line)


# 判断文件中a出现的次数

count_a = 0
with open('file/pi_digits.txt') as file_object:
    lines=file_object.readlines()
    for line in lines:
        index = 0
        find_index = 0
        while index != -1:
            index = line.find("a",find_index)
            if index != -1:
                count_a = count_a + 1
                find_index = index + 1
print(count_a)

# 将字符串转变为了列表，遍历其中元素if判断进行统计
# sum = 0
# with open('pi_digits.txt') as file_object:
#     for line in file_object:
#         for Q in list(line):
#             if Q == "a":
#                 sum+=1
# print(sum)

# 读取模式 （'r' ）
# 写入模式 （'w' ）
# 附加模式 （'a' ）
# 让你能够读取和写入文件的模式（'r+')

# 写入文件
with open("file/programe.txt","w") as file_object:
    file_object.write("i love")